import soccerbots

cdef extern from "ai_common.h":
    ctypedef struct c_RobotState "probot":
        int row
        int col
        int orientation
        int robot_id
        int linearVel
        int angVel

    ctypedef struct c_BallState "pball":
        int row
        int col
        int linearVel

    ctypedef struct c_GameState "stateGame":
        c_RobotState ours[5]
        c_RobotState theirs[5]
        c_BallState ball
        int win
        int round

    ctypedef struct c_Movement "movements":
        int vel_lin[5]
        int vel_ang[5]

    cdef int c_ProcessState "processState" (c_GameState *state, c_Movement *mov)

cdef extern from "ai_move.h":
    cdef void c_setMaxLinearVel "ai_set_MaxLin_Vel" (int vel)
    cdef void c_setMaxAngularVel "ai_set_MaxAng_Vel" (int vel)

class AI:
    def setMaxLinearVel(self, vel):
        self.max_linear_vel = vel
        c_setMaxLinearVel(vel)

    def setMaxAngularVel(self, vel):
        self.max_ang_vel = vel
        c_setMaxAngularVel(vel)

    def acotar(self, val, max):
        if val > max:
            return max
        elif val > 0:
            return val
        else:
            return 0

    def processState(self, state, team=1):
        cdef c_GameState gamestate
        cdef c_Movement movement
        gamestate.win = 0
        gamestate.round = state.round
        gamestate.ball.col = state.ball.position[0]
        gamestate.ball.row = state.ball.position[1]
        gamestate.ball.linearVel = state.ball.linearVel
        i = 0
        print 'CACA:'
        for k in state.teamA.keyset:
            print state.teamA[k].position
            gamestate.ours[i].robot_id = i
            gamestate.ours[i].col = state.teamA[k].position[0]
            gamestate.ours[i].row = state.teamA[k].position[1]
            gamestate.ours[i].orientation = state.teamA[k].direction
            gamestate.ours[i].linearVel = state.teamA[k].linearVel
            gamestate.ours[i].angVel = state.teamA[k].angVel
            i = i+1
        i = 0
        for k in state.teamB.keyset:
            print state.teamB[k].position
            gamestate.theirs[i].robot_id = i
            gamestate.theirs[i].col = state.teamB[k].position[0]
            gamestate.theirs[i].row = state.teamB[k].position[1]
            gamestate.theirs[i].orientation = state.teamB[k].direction
            gamestate.theirs[i].linearVel = state.teamB[k].linearVel
            gamestate.theirs[i].angVel = state.teamB[k].angVel
            i = i+1
        c_ProcessState(&gamestate, &movement)
        mov = soccerbots.core.TeamMovement()
        i = 0
        for k in mov.keyset:
            mov[k] = soccerbots.core.PlayerMovement()
            mov[k].linearVelocity = self.acotar(movement.vel_lin[i], self.max_linear_vel)
            mov[k].angVelocity = self.acotar(movement.vel_ang[i], self.max_ang_vel)
            i = i+1
        
        return mov
